package com.test.controller;

import com.test.entity.Htong;
import com.test.entity.Msg;
import com.test.service.HtongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@CrossOrigin
public class HtongController {

    @Autowired
    HtongService htongService;

//查询
    @RequestMapping(value = "/getHtongList", method = RequestMethod.GET)
    @ResponseBody
    public Msg selectAll(){
        List<Htong> htongs = htongService.selectAll();
        return Msg.success().add("htong", htongs);
    }

    @RequestMapping(value = "/getHtongByDate", method = RequestMethod.GET)
    @ResponseBody
    public Msg selectByDate(@RequestParam(value = "startDate")String startDate, @RequestParam(value = "endDate")String endDate){
        if (startDate != null && endDate != null){
            List<Htong> htongs = htongService.selectByData(startDate, endDate);
            return Msg.success().add("htong", htongs);
        }else {
            List<Htong> htongs = htongService.selectAll();
            return Msg.success().add("htong", htongs);
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public Msg delete(@RequestParam(value = "bhao")String bhao){
        int delete = htongService.delete(bhao);
        if (delete > 0){
            return Msg.success();
        }else {
            return Msg.fail();
        }
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public Msg insert(@RequestBody Htong htong){
        int result = htongService.addHtong(htong);
        if (result > 0){
            return Msg.success();
        }else {
            return Msg.fail();
        }
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    @ResponseBody
    public Msg edit(@RequestParam(value = "bhao")String bhao){
        Htong htong = htongService.selectBybhao(bhao);
        if (htong != null){
            return Msg.success().add("htong", htong);
        }else {
            return Msg.fail();
        }
    }

    @RequestMapping(value = "/editUpdate", method = RequestMethod.POST)
    @ResponseBody
    public Msg editUpdate(@RequestBody Htong htong){
        int result = htongService.updateHtong(htong);
        if (result > 0){
            return Msg.success();
        }else {
            return Msg.fail();
        }
    }
}
